package com.globallogic.draw;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class DrawContainer implements Iterable<Drawable> {

	public static DrawContainer getInstance() {
		
		if (null == drawContainer) {
			
			drawContainer = new DrawContainer();
			
		}
		
		return drawContainer;
		
	}
	
	public void clear(){
		
		drawObjectList.clear();
		
	}
	
	private DrawContainer() {
		
		drawObjectList = new ArrayList<Drawable>();
		
	}
	
	public void addDrawObject(Drawable drawObject) {
		
		drawObjectList.add(drawObject);
		
	}
	
	private List<Drawable> drawObjectList;

	private int iteratorIndex = -1;

	@Override
	public Iterator<Drawable> iterator() {
		
		return new DrawContainerIterator();
		
	}
	
	class DrawContainerIterator implements Iterator<Drawable> {

		@Override
		public boolean hasNext() {

			boolean hasNextItem;
			
			hasNextItem = (drawObjectList.size() > 0) && (iteratorIndex < drawObjectList.size() - 1);
			
			if (!hasNextItem) {
				
				iteratorIndex = -1;
			
			}
			
			return hasNextItem;
			
		}

		@Override
		public Drawable next() {
			
			iteratorIndex++;
			return drawObjectList.get(iteratorIndex);
			
		}

		@Override
		public void remove() {

			throw new UnsupportedOperationException();
			
		}

	} 
	
	private static DrawContainer drawContainer = null;
	
}

